home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Dema / betonsoldier_spdemo.exe / {app} / Shaders / mesh_clouds_bump.fx < prev    next >
Encoding:
Text File  |  2005-07-05  |  19.0 KB  |  568 lines

  1. //------------------------------------------------------------------------------------------------------
  2. //------------------------------------------------------------------------------------------------------
  3. //------------------------------------------------------------------------------------------------------
  4.  
  5. #include "shared.fx"
  6. #include "lighting.fx"
  7. #include "fog.fx"
  8.  
  9. //------------------------------------------------------------------------------------------------------
  10. //------------------------------------------------------------------------------------------------------
  11. //------------------------------------------------------------------------------------------------------
  12.  
  13. //------------------------------------------------------------------------------------------------------
  14. //- Static parameters
  15. //------------------------------------------------------------------------------------------------------
  16.  
  17. texture                DiffuseMap;
  18. texture                NormalMap;
  19. texture                GlossinessMap;
  20. texture                SpecularMap;
  21. shared float    Time;  // a float that ranges up from 0.0f to 1.0f cycling for time-based effects
  22.  
  23. //------------------------------------------------------------------------------------------------------
  24. //------------------------------------------------------------------------------------------------------
  25. //------------------------------------------------------------------------------------------------------
  26.  
  27. struct    CVertexShaderInput
  28. {
  29.     float4    Position        :    POSITION;
  30.     float3    Normal            :    NORMAL;    
  31.     float2    DiffuseUv        :    TEXCOORD0;    
  32.     float3    BumpTangent    :    TEXCOORD1;    
  33.     float3    BumpNormal    :    TEXCOORD2;
  34. };
  35.  
  36. //------------------------------------------------------------------------------------------------------
  37.  
  38. struct    CVertexShaderOutput
  39. {
  40.     float4    Position            :    POSITION;
  41.     float2    DiffuseUv            :    TEXCOORD0;    
  42.     float2    NormalUv            :    TEXCOORD1;
  43.     float3    TextureSpaceLightVector1    :    COLOR0;    
  44.     float3    TextureSpaceLightVector2    :    COLOR1;
  45.     FOG_OPTION_VERTEX_FIELD
  46. };
  47.  
  48. //------------------------------------------------------------------------------------------------------
  49.  
  50. struct    CVertexShaderOutputTech2
  51. {
  52.     float4    Position                :    POSITION;
  53.     float2    DiffuseUv                :    TEXCOORD0;    
  54.     float3    DiffuseColor        :    COLOR0;
  55.     FOG_OPTION_VERTEX_FIELD
  56. };
  57.  
  58. //------------------------------------------------------------------------------------------------------
  59.  
  60. struct    CVertexShaderOutputSpec
  61. {
  62.     float4    Position                                        :    POSITION;
  63.     float2    DiffuseUv                                        :    TEXCOORD0;    
  64.     float2    NormalUv                                        :    TEXCOORD1;
  65.     float3    TextureSpaceLightVector            :    TEXCOORD2;    
  66.     float3    TextureSpaceLightVectorHalf    :    TEXCOORD3;    
  67.     float3    SpecularStrength                        :    COLOR0;
  68.     FOG_OPTION_VERTEX_FIELD_HACK(COLOR1)
  69. };
  70.  
  71. //------------------------------------------------------------------------------------------------------
  72. //------------------------------------------------------------------------------------------------------
  73. //------------------------------------------------------------------------------------------------------
  74.  
  75. // ambient + 2 omnis + bump + specular (2 passes : diffuse + specular)
  76.  
  77. CVertexShaderOutput                OpaBumpLitGeomTech1Pass1    (const CVertexShaderInput input);
  78. CVertexShaderOutputSpec        OpaBumpLitGeomTech1Pass2    (const CVertexShaderInput input);
  79. CVertexShaderOutputTech2    OpaBumpLitGeomTech2                (const CVertexShaderInput input);
  80.  
  81. //------------------------------------------------------------------------------------------------------
  82. //------------------------------------------------------------------------------------------------------
  83. //------------------------------------------------------------------------------------------------------
  84. #define DISPLACEMENT_FACTOR_TANGENT 5.0f
  85. #define DISPLACEMENT_FACTOR_NORMAL  1.0f
  86. #define MY_2_PI    6.28318531f
  87. #define PHASE 1.0f
  88.  
  89. /*
  90. float4 AddNoise(const float4 vPositionToNoise)
  91. {
  92.     float rSinTime = 1.0f + sin(MY_2_PI * PHASE * Time);    
  93.     rSinTime /= 2.0f;
  94.     float4 ToBeReturned = vPositionToNoise;
  95.     ToBeReturned *= (1.0f + rSinTime);
  96.     ToBeReturned.w = vPositionToNoise.w;
  97.     
  98.     return ToBeReturned;
  99. }    
  100.     float rCosTime = cos(MY_2_PI * PHASE * Time);    
  101.     float rSinTime = abs(sin(MY_2_PI * PHASE * Time));    
  102.     
  103.     // add a random component based on actual position...
  104.     float4 ToBeReturned = vPositionToNoise;
  105.     ToBeReturned.xyz += rVertexRandom * rCosTime * vTangent * DISPLACEMENT_FACTOR_TANGENT;
  106.     ToBeReturned.xyz += rVertexRandom * rSinTime * vNormal  * DISPLACEMENT_FACTOR_NORMAL;
  107.     return ToBeReturned;
  108. */    
  109.  
  110.  
  111. CVertexShaderOutput    OpaBumpLitGeomTech1Pass1 (const CVertexShaderInput input)
  112. {
  113.     CVertexShaderOutput output;
  114.     
  115.     // output position into world+view+projection space    
  116.     output.Position = mul (input.Position,WorldCameraProjection); 
  117.     
  118.     // binormal computation based on input tangent and normal in local space    
  119.     //float3 binormal = ComputeBinormal (input.BumpTangent,input.BumpNormal,input.Normal);
  120.     float3 binormal = input.Normal;
  121.  
  122.     float3 omniDirectionTextureSpace[2];
  123.     
  124.     for (int i=0;i<2;i++)
  125.     {
  126.         // omnilight direction computation
  127.         float4 omniDirection = ComputeVectorAndLength (ObjectLocalLightPosition[i],input.Position);
  128.         
  129.         // omnilight direction to texture space
  130.         omniDirectionTextureSpace[i] = TextureSpaceTransform (omniDirection.xyz,input.BumpTangent,input.BumpNormal,binormal);
  131.         omniDirectionTextureSpace[i] *= ComputeAttenuation (omniDirection.w,LightAttenuationFarStart[i],LightAttenuationFarEnd[i],LightAttenuationDelta[i]);
  132.     }
  133.     
  134.     // omnilight 1 output to diffuse
  135.     output.TextureSpaceLightVector1.xyz = omniDirectionTextureSpace[0] * 0.5f + 0.5f;
  136.     
  137.     // omnilight 2 output to diffuse
  138.     output.TextureSpaceLightVector2.xyz = omniDirectionTextureSpace[1] * 0.5f + 0.5f;
  139.     
  140.     // Diffuse Uv output
  141.     output.DiffuseUv = input.DiffuseUv;
  142.         
  143.     // Normal Uv output
  144.     output.NormalUv = input.DiffuseUv;
  145.         
  146.     // fog computation
  147.     FOG_OPTION_COMPUTE(output, output.Position);
  148.  
  149.     return output;
  150. }
  151.  
  152. //------------------------------------------------------------------------------------------------------
  153. //------------------------------------------------------------------------------------------------------
  154. //------------------------------------------------------------------------------------------------------
  155.  
  156. CVertexShaderOutputSpec    OpaBumpLitGeomTech1Pass2 (const CVertexShaderInput input)
  157. {
  158.     CVertexShaderOutputSpec output;
  159.     
  160.     // output position into world+view+projection space
  161.     output.Position = mul (input.Position,WorldCameraProjection);
  162.  
  163.     // binormal computation based on input tangent and normal in local space
  164.     float3 binormal = input.Normal;
  165.     
  166.     // Camera vertex direction
  167.     float3 cameraDirection = ComputeVector (ObjectLocalCameraPosition,input.Position);
  168.     
  169.     // Light vertex direction
  170.     float4 omniDirection1 = ComputeVectorAndLength (ObjectLocalLightPosition[0],input.Position);
  171.     
  172.     // half view vector computation
  173.     float3 halfView1 = ComputeHalfViewVector (omniDirection1.xyz,cameraDirection);
  174.     //float3 halfView1 = reflect (-omniDirection1.xyz,cameraDirection);
  175.         
  176.     // omni direction to texture space
  177.     float3 omniDirectionTextureSpace = TextureSpaceTransform (omniDirection1.xyz,input.BumpTangent,input.BumpNormal,binormal);
  178.         
  179.     output.TextureSpaceLightVector = omniDirectionTextureSpace * 0.5f + 0.5f;
  180.             
  181.     // half view vector to texture space
  182.     float3 halfView1TextureSpace = TextureSpaceTransform (halfView1,input.BumpTangent,input.BumpNormal,binormal);
  183.     
  184.     // half view vector 1 output to diffuse
  185.     output.TextureSpaceLightVectorHalf = halfView1TextureSpace * 0.5f + 0.5f;
  186.     
  187.     // Diffuse Uv output
  188.     output.DiffuseUv = input.DiffuseUv;
  189.     
  190.     // Normal Uv output
  191.     output.NormalUv = input.DiffuseUv;
  192.     
  193.     output.SpecularStrength = dot (halfView1TextureSpace,binormal);
  194.     output.SpecularStrength *= ComputeAttenuation (omniDirection1.w,LightAttenuationFarStart[0],LightAttenuationFarEnd[0],LightAttenuationDelta[0]);
  195.     
  196.     // fog computation
  197.     FOG_OPTION_ON(float rFog = fog_linear(output.Position); output.Hack = float4(rFog,rFog,rFog,rFog);)
  198.  
  199.     return output;
  200. }
  201.  
  202. //------------------------------------------------------------------------------------------------------
  203. //------------------------------------------------------------------------------------------------------
  204. //------------------------------------------------------------------------------------------------------
  205.  
  206. CVertexShaderOutputTech2    OpaBumpLitGeomTech2 (const CVertexShaderInput input)
  207. {
  208.     CVertexShaderOutputTech2 output;
  209.     
  210.     // output position into world+view+projection space
  211.     output.Position = mul (input.Position,WorldCameraProjection);
  212.     
  213.     // normal computation based on input tangent and normal in local space
  214.     float3 normal = input.Normal;
  215.     
  216.     float3 computedDiffuseColors[2];
  217.     
  218.     for (int i=0;i<2;i++)
  219.     {
  220.         // omnilight direction computation
  221.         float4 omniDirection = ComputeVectorAndLength (ObjectLocalLightPosition[i],input.Position);
  222.         
  223.         // omnilight color computation
  224.         float intensity = dot (omniDirection.xyz,normal);
  225.         intensity = max(intensity,0.0f);
  226.         intensity = min(intensity,1.0f);
  227.         intensity *= ComputeAttenuation (omniDirection.w,LightAttenuationFarStart[i],LightAttenuationFarEnd[i],LightAttenuationDelta[i]);
  228.         computedDiffuseColors[i].xyz = intensity * DiffuseColor[i];
  229.         
  230.         //computedDiffuseColors[i].xyz = dot (omniDirection.xyz,normal);
  231.         //computedDiffuseColors[i] *= ComputeAttenuation (omniDirection.w,LightAttenuationFarStart[i],LightAttenuationFarEnd[i],LightAttenuationDelta[i]);
  232.         //computedDiffuseColors[i] *= DiffuseColor[i];
  233.     }
  234.     
  235.     // Diffuse Uv output
  236.     output.DiffuseUv = input.DiffuseUv;
  237.         
  238.     // Diffuse color output
  239.     output.DiffuseColor = computedDiffuseColors[0] + computedDiffuseColors[1];
  240.         
  241.     // fog computation
  242.     FOG_OPTION_COMPUTE(output, output.Position);
  243.  
  244.     return output;
  245. }
  246.  
  247. //------------------------------------------------------------------------------------------------------
  248. //------------------------------------------------------------------------------------------------------
  249. //------------------------------------------------------------------------------------------------------
  250.  
  251. sampler DiffuseSampler = sampler_state
  252. {
  253.     Texture        = <DiffuseMap>;
  254.     MinFilter    = LINEAR;
  255.     MagFilter    = LINEAR;
  256.     MipFilter    = LINEAR;
  257.     AddressU    = WRAP;
  258.     AddressV    = WRAP;
  259. };
  260.  
  261. //------------------------------------------------------------------------------------------------------
  262. //------------------------------------------------------------------------------------------------------
  263. //------------------------------------------------------------------------------------------------------
  264.  
  265. sampler NormalSampler = sampler_state
  266. {
  267.     Texture        = <NormalMap>;
  268.     MinFilter    = LINEAR;
  269.     MagFilter    = LINEAR;
  270.     MipFilter    = LINEAR;
  271.     AddressU    = WRAP;
  272.     AddressV    = WRAP;
  273. };
  274.  
  275. //------------------------------------------------------------------------------------------------------
  276. //------------------------------------------------------------------------------------------------------
  277. //------------------------------------------------------------------------------------------------------
  278.  
  279. sampler SpecularSampler = sampler_state
  280. {
  281.     Texture        = <SpecularMap>;
  282.     MinFilter    = LINEAR;
  283.     MagFilter    = LINEAR;
  284.     MipFilter    = LINEAR;
  285.     AddressU    = WRAP;
  286.     AddressV    = WRAP;
  287. };
  288.  
  289. //------------------------------------------------------------------------------------------------------
  290. //------------------------------------------------------------------------------------------------------
  291. //------------------------------------------------------------------------------------------------------
  292.  
  293. sampler GlossinessSampler = sampler_state
  294. {
  295.     Texture        = <GlossinessMap>;
  296.     MinFilter    = LINEAR;
  297.     MagFilter    = LINEAR;
  298.     MipFilter    = LINEAR;
  299.     AddressU    = CLAMP;
  300.        AddressV    = CLAMP;
  301. };
  302.  
  303. //------------------------------------------------------------------------------------------------------
  304. //------------------------------------------------------------------------------------------------------
  305. //------------------------------------------------------------------------------------------------------
  306.  
  307. #define Pass1RenderStateBlock \
  308. CullMode            = CW; \
  309. ZEnable                = true; \
  310. ZFunc                = LESSEQUAL; \
  311. ZWriteEnable        = false; \
  312. AlphaBlendEnable    = true; \
  313. SrcBlend            = SRCALPHA; \
  314. DestBlend            = INVSRCALPHA; \
  315. FOG_OPTION_PARAMETERS
  316.  
  317. //------------------------------------------------------------------------------------------------------
  318.  
  319. #define Pass2RenderStateBlock \
  320. CullMode            = CW; \
  321. ZEnable                = true; \
  322. ZFunc                = LESSEQUAL; \
  323. ZWriteEnable        = false; \
  324. AlphaBlendEnable    = true; \
  325. SrcBlend            = ONE; \
  326. DestBlend            = ONE; \
  327. FogEnable            = false
  328.  
  329.  
  330. #define Pass1RenderStateBlockTech3 \
  331. CullMode            = CW; \
  332. ZEnable                = true; \
  333. ZFunc                    = LESSEQUAL; \
  334. ZWriteEnable        = false; \
  335. AlphaBlendEnable    = false; \
  336. FOG_OPTION_PARAMETERS
  337.  
  338.  
  339. //------------------------------------------------------------------------------------------------------
  340. //------------------------------------------------------------------------------------------------------
  341. //------------------------------------------------------------------------------------------------------
  342.  
  343. technique    tech1
  344. <
  345.     int Priority = 1;
  346.     int NeedSorting = 1;
  347.     int TechniqueIndex = 0;
  348.     int DeviceType = HWSHADER_ONLY;
  349.     int LightingType = INTEGRATED_LIGHTING;
  350.     string RenderingType = "Standard";
  351. >
  352. {
  353.     pass pass1
  354.     {
  355.         Sampler[0]    = <DiffuseSampler>;
  356.         Sampler[1]    = <NormalSampler>;
  357.  
  358.         Pass1RenderStateBlock;
  359.         
  360.         VertexShader = compile vs_1_1 OpaBumpLitGeomTech1Pass1();
  361.         
  362.         PixelShaderConstant[0]        = <AmbientColor>;
  363.         PixelShaderConstant[1]        = <DiffuseColor[0]>;
  364.         PixelShaderConstant[3]        = <DiffuseColor[1]>;
  365.         
  366.         PixelShader = 
  367.         asm
  368.         {
  369.             ps_1_1
  370.             
  371.             tex            t0    // Diffuse map
  372.             tex            t1    // Normal map
  373.  
  374.             dp3_sat r0.rgb,t1_bx2,v0_bx2 +mov_sat r0.a,t0.a // r1 = dp3 (light1 * normal)
  375.             dp3_sat r1.rgb,t1_bx2,v1_bx2 +mov_sat r1.a,t0.a // r1 = dp3 (light1 * normal)
  376.             mul r0.rgb,r0,c1
  377.             mad r0.rgb,r1,c3,r0
  378.             mul_x2_sat r0.rgb,r0,t0
  379.             mad_sat r0.rgb,t0,c0,r0
  380.         };
  381.     }
  382.  
  383.     pass pass2
  384.     {
  385.         Sampler[0]    = <SpecularSampler>;
  386.         Sampler[1]    = <NormalSampler>;
  387.         Sampler[3]    = <GlossinessSampler>;
  388.         
  389.     Pass2RenderStateBlock;
  390.         
  391.         VertexShader = compile vs_1_1 OpaBumpLitGeomTech1Pass2();
  392.         
  393.         PixelShaderConstant[2]        = <SpecularColor>;
  394.         
  395.         PixelShader = 
  396.         asm
  397.         {
  398.             ps_1_1
  399.             
  400.             tex            t0    // Specular map
  401.             tex            t1    // Normal map
  402.             texm3x2pad    t2, t1_bx2 // Light vector * normal vector
  403.             texm3x2tex    t3, t1_bx2 // Half vector * normal vector
  404.             
  405.             mov r0.rgb,t3 + mov r0.a,t0.a
  406.             mul r0.rgb,r0,v0
  407.             mul r0.rgb,r0,t0
  408.             mul_x4 r0.rgb,r0,c2
  409.             
  410.             // fog hack
  411.             mul_sat r0.rgb, r0, v1
  412.         };
  413.     }
  414. }
  415.  
  416. //------------------------------------------------------------------------------------------------------
  417. //------------------------------------------------------------------------------------------------------
  418. //------------------------------------------------------------------------------------------------------
  419.  
  420. technique    tech2
  421. <
  422.     int Priority = 1;
  423.     int TechniqueIndex = 1;
  424.     int DeviceType = HWSHADER_ONLY;
  425.     int LightingType = INTEGRATED_LIGHTING;
  426.     string RenderingType = "Standard";
  427. >
  428. {
  429.     pass pass1
  430.     {
  431.         Sampler[0]    = <DiffuseSampler>;
  432.         Sampler[1]    = <NormalSampler>;
  433.         
  434.         Pass1RenderStateBlock;
  435.         
  436.         VertexShader = compile vs_1_1 OpaBumpLitGeomTech1Pass1();
  437.         
  438.         PixelShaderConstant[0]        = <AmbientColor>;
  439.         PixelShaderConstant[1]        = <DiffuseColor[0]>;
  440.         PixelShaderConstant[3]        = <DiffuseColor[1]>;
  441.         
  442.         PixelShader = 
  443.         asm
  444.         {
  445.             ps_1_1
  446.             
  447.             tex            t0    // Diffuse map
  448.             tex            t1    // Normal map
  449.             
  450.             dp3_sat r0.rgb,t1_bx2,v0_bx2 +mov_sat r0.a,t0.a // r1 = dp3 (light1 * normal)
  451.             dp3_sat r1.rgb,t1_bx2,v1_bx2 +mov_sat r1.a,t0.a // r1 = dp3 (light1 * normal)
  452.             mul r0.rgb,r0,c1
  453.             mad r0.rgb,r1,c3,r0
  454.             mul_x2_sat r0.rgb,r0,t0
  455.             mad_sat r0.rgb,t0,c0,r0
  456.         };
  457.     }
  458. }
  459.  
  460. //------------------------------------------------------------------------------------------------------
  461. //------------------------------------------------------------------------------------------------------
  462. //------------------------------------------------------------------------------------------------------
  463.  
  464. technique    tech3
  465. <
  466.     int Priority = 1;
  467.     int TechniqueIndex = 2;
  468.     int DeviceType = HWSHADER_ONLY;
  469.     int LightingType = INTEGRATED_LIGHTING;
  470.     string RenderingType = "Standard";
  471. >
  472. {
  473.     pass pass1
  474.     {
  475.         Sampler[0]    = <DiffuseSampler>;
  476.         
  477.         //Pass1RenderStateBlock;
  478.         Pass1RenderStateBlockTech3;
  479.         
  480.         VertexShader = compile vs_1_1 OpaBumpLitGeomTech2();
  481.         
  482.         PixelShaderConstant[0]        = <AmbientColor>;
  483.         
  484.         PixelShader = 
  485.         asm
  486.         {
  487.             ps_1_1
  488.             
  489.             tex            t0    // Diffuse map
  490.             //tex            t1  // fog hack
  491.  
  492.             mul_x2_sat r0.rgb,v0,t0  +mov r0.a,t0.a
  493.             mad_sat r0.rgb,t0,c0,r0
  494.         };
  495.     }
  496. }
  497.  
  498. //------------------------------------------------------------------------------------------------------
  499. //------------------------------------------------------------------------------------------------------
  500. //------------------------------------------------------------------------------------------------------
  501.  
  502. technique    techTnL_0
  503. <
  504.     int Priority = 1;
  505.     int TechniqueIndex = 0;
  506.     int DeviceType = TNL_ONLY;
  507.     int LightingType = INTEGRATED_LIGHTING;
  508.     string RenderingType = "Standard";
  509. >
  510. {
  511.     pass pass1
  512.     {
  513.         WorldTransform[0]        = <WorldCameraProjection>;
  514.         
  515.         LightType[0]                = POINT;
  516.         LightAttenuation0[0]    = <LightAttenuationFarStart[0]>;
  517.         LightAttenuation1[0]    = 0.0f;
  518.         LightAttenuation2[0]    = <LightAttenuationFarEnd[0]>;
  519.         LightAmbient[0]            = <AmbientColor>;
  520.         LightDiffuse[0]            = <DiffuseColor[0]>;
  521.         LightPosition[0]        = <ObjectLocalLightPosition[0]>;
  522.         LightRange[0]                = <LightAttenuationDelta[0]>;
  523.         LightEnable[0]            = true;
  524.         LightEnable[1]            = false;
  525.         LightEnable[2]            = false;
  526.         LightEnable[3]            = false;
  527.         LightEnable[4]            = false;
  528.         LightEnable[5]            = false;
  529.         LightEnable[6]            = false;
  530.         LightEnable[7]            = false;
  531.         Lighting                        = true;
  532.         MaterialAmbient            = {1.0f,1.0f,1.0f,1.0f};
  533.         MaterialDiffuse            = {1.0f,1.0f,1.0f,1.0f};
  534.         MaterialEmissive        = {0.0f,0.0f,0.0f,0.0f};
  535.         MaterialPower                = 0.0f;
  536.         MaterialSpecular        = {0.0f,0.0f,0.0f,0.0f};
  537.         DiffuseMaterialSource    = COLOR1;
  538.         AmbientMaterialSource    = MATERIAL;
  539.         
  540.         Sampler[0]    = <DiffuseSampler>;
  541.         
  542.         ColorArg1[0]        = TEXTURE;
  543.         ColorArg2[0]        = DIFFUSE;
  544.         ColorOp[0]            = MODULATE2X;
  545.         AlphaArg1[0]        = TEXTURE;
  546.         AlphaOp[0]            = SELECTARG1;
  547.         ColorOp[1]            = DISABLE;
  548.         AlphaOp[1]            = DISABLE;
  549.         
  550.         Pass1RenderStateBlock;
  551.         
  552.         VertexShader = NULL;
  553.         
  554.         PixelShader = NULL;
  555.     }
  556. }
  557.  
  558. //------------------------------------------------------------------------------------------------------
  559. //------------------------------------------------------------------------------------------------------
  560. //------------------------------------------------------------------------------------------------------
  561.  
  562. #include "mesh_shadow.fx"
  563. #include "mesh_shadow_projector.fx"
  564.  
  565. //------------------------------------------------------------------------------------------------------
  566. //------------------------------------------------------------------------------------------------------
  567. //------------------------------------------------------------------------------------------------------
  568.